import requests
from bs4 import BeautifulSoup

class csbk():
    def __init__(self):
        self.pageIndex=1
        self.stories=[]
        self.enable=False
    def get_story(self,pageIndex):
        headers = {
            'User-Agent': "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1"}
        start_url = 'https://www.qiushibaike.com/textnew/page/' + str(pageIndex) + '/?s=5033464'
        start_html = requests.get(start_url, headers=headers)
        Soup = BeautifulSoup(start_html.text, 'html5lib').find_all('div', class_='content')
        pagestories = []
        for list in Soup:
            text = list.get_text().strip()
            pagestories.append(text)
        return pagestories
    def loadpage(self):
        if self.enable==True:
            if(len(self.stories)<2):
                pageStories=self.get_story(self.pageIndex)
                if pageStories:
                    self.stories.append(pageStories)
                    self.pageIndex +=1
    def getonestory(self,pagestories):
        for story in pagestories:
            input1=input('请按回车查看下一条段子：')
            self.loadpage()
            if input1=='q':
                self.enable=False
            print(story)
    def start(self):
        print('正在读取糗事百科段子，按回车查看新段子，按q退出')
        self.enable=True
        self.loadpage()
        nowpage=0
        while self.enable:
            if len(self.stories)>0:
                pagestories=self.stories[0]
                del self.stories[0]
                self.getonestory(pagestories)
spider = csbk()
spider.start()